﻿
using HBase; 
using hconnect;
using Net.http;
using System;
using System.Collections.Generic;
namespace hCommand {
    public abstract class AbsCarrierSynchro<T>  : AbsSynchro where T : Signal {

       
        public override MyDict<int, TaskStatusPersistence> getTaskNo(AbsStatus status) {//调用在一个线程中一定是序列化的所以不用加所
            SignalStatus<T> cstatus = (SignalStatus<T>)status;
            Dictionary<int, T> c = cstatus.c;

            MyDict<int, TaskStatusPersistence> dict = new MyDict<int, TaskStatusPersistence>();
            foreach (KeyValuePair<int, T> kvp in c) {//键值对一起遍历
                T carrierSignal = kvp.Value;
                int taskNo = carrierSignal.no;
                BaseTask bt = getTask(carrierSignal);
                if (bt == null) {
                    continue;
                }
                CoreTaskParamStatus st = CoreTaskParamStatus.取货行走中;
                int p = carrierSignal.p;
                if (carrierSignal.isArrived() && Math.Abs(bt.des)   ==p) {
                    st = CoreTaskParamStatus.执行结束;
                    p = bt.des;
                }
                if (dict.ContainsKey(taskNo)) {
                    if (dict[taskNo].status < (int)st) {
                        dict.addOverride(taskNo, new TaskStatusPersistence(taskNo, p, bt, st, ""));
                    }
                } else {
                    dict.addIgnores(taskNo, new TaskStatusPersistence(taskNo, p, bt, st, ""));
                }
            }
            return dict;
        }
        BaseTask getTask(T carrierSignal) {
            int taskNo = carrierSignal.no;
            if (taskNo == 0) {
                return null;
            }
            CoreTaskParam param = MyCache.queryUnique<CoreTaskParam>((a) => {
                return a.taskNo == taskNo;
            }, "CompleteParam 过多" + taskNo);

            if (param == null) {
                return null;
            }
            Node node = Node.getConfig(carrierSignal.p);
            Estimate e = node.getTaskEstimate(param.taskNo);
            if (e == null) {
                return null;
            }
            BaseTask bt = PathFacade.getTaskById(e.taskId);
            return bt;

        }

        protected override void after(AbsStatus cs) {
            SignalStatus<T> cstatus = (SignalStatus<T>)cs;
            Dictionary<int, T> c = cstatus.c;
            foreach (var kw in c) {
                T t = kw.Value;
                if (t.no == 0) {
                    continue;
                }
                Node node = Node.getConfig(kw.Key);
                if (node == null) {

                    continue;
                }
                node.taskArrived(t.no);
            }
        }
    }
}
